package com.cloudcc.cloudframe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.cloudcc.cloudframe.util.ListUtils;
import com.litesuits.android.log.Log;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author wangzhiqiang
 * @date 2015-7-10 上午9:17:48
 *
 */
public abstract class CommonGridAdapter<T> extends BaseAdapter {
    public int conut = 0;
    protected List<T> mData;
    protected Context mContext;
    protected LayoutInflater mInflater;

    public CommonGridAdapter(Context mContext) {
        super();
        mData = new ArrayList<T>();
        this.mContext = mContext;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return ListUtils.getSize(mData);
    }

    @Override
    public T getItem(int position) {
        return mData.get(position);
    }

    /**
     * 添加数据集
     *
     * @param data
     */
    public void addData(List<T> data) {
        if (mData == null) {
            mData = new ArrayList<T>();
        }
        mData.addAll(data);
        conut = 0;
        notifyDataSetChanged();
        Log.d("istrue", "过来了");
    }

    /**
     * 添加单条数据
     *
     * @param t
     */
    public void addItem(T t) {
        if (mData == null) {
            mData = new ArrayList<T>();
        }
        mData.add(t);
        conut = 0;
        notifyDataSetChanged();
    }

    /**
     * 在指定位置添加一条数据
     *
     * @param pos
     * @param obj
     */
    public void addItem(int pos, T obj) {
        if (mData == null) {
            mData = new ArrayList<T>();
        }
        mData.add(pos, obj);
        conut = 0;
        notifyDataSetChanged();
    }

    /**
     * 移除一条数据
     *
     * @param obj
     */
    public void removeItem(T obj) {
        mData.remove(obj);
        conut = 0;
        notifyDataSetChanged();
    }

    /**
     * 移除一条记录
     *
     * @param pos
     */
    public void removeItem(int pos) {
        mData.remove(pos);
        conut = 0;
        notifyDataSetChanged();
    }

    /**
     * 清空数据
     */
    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        conut = 0;
        notifyDataSetChanged();
    }

    public void changeData(List<T> data) {
        if (mData != null) {
            mData.clear();
            mData = null;
        }
        mData = data;
        Log.d("isnull", "is:" + mData != null + "::" + data.toString() + "::"
                + (data != null));
        conut = 0;
        notifyDataSetChanged();
    }

    /**
     * @return
     */
    public List<T> getData() {
        return mData;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = ViewHolder.getg(mContext, convertView, parent,
                getLayoutId(), position);
        convert(holder, getItem(position));
        return holder.getConvertView();
    }

    /**
     * 将view的数据跟view绑定
     *
     * @param holder
     * @param t
     */
    public abstract void convert(ViewHolder holder, T t);


    /**
     * 获得布局
     *
     * @return
     */
    public abstract int getLayoutId();



}
